package com.example.yunhailutong.zhongtan.weight;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import com.example.yunhailutong.zhongtan.R;


/**
 * Waiting 等待对话框
 */
public class LoadingDialog extends Dialog {

    private Context context = null;
    private static LoadingDialog loadingDialog = null;
    //任务数
    private int taskNum = 0;

    public LoadingDialog(Context context) {
        super(context);
        this.context = context;
    }

    public LoadingDialog(Context context, int theme) {
        super(context, theme);
    }

    public static LoadingDialog createDialog(Context context) {
        loadingDialog = new LoadingDialog(context, R.style.dialog_progress);
        loadingDialog.setContentView(R.layout.view_loading_dialog);
        loadingDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        return loadingDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (loadingDialog == null) {
            return;
        }
        final View v = loadingDialog.findViewById(R.id.v_dialog_loadinng);
   //     Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.rm);
        Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setRepeatCount(-1);//一直循环
        animation.setDuration(1000);
        LinearInterpolator lin = new LinearInterpolator();
        animation.setInterpolator(lin);
        v.startAnimation(animation);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }

    /**
     * [Summary]
     * setTitile 标题
     *
     * @param strTitle
     * @return
     */
    public LoadingDialog setTitile(String strTitle) {
        return loadingDialog;
    }


    public void showAddTask(int i) {
        this.taskNum += i;
        if (!this.isShowing()) {
            this.show();
        }
    }

    public void dismissForTask() {
        this.taskNum--;
        if (taskNum <= 0) {
            dismiss();
        }
    }

    public void stopTask() {
        taskNum = 0;
        dismiss();
    }


}
